
#include "three_nn_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"


namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

    ThreeNNTilingData tiling;

    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    uint64_t ub_size; 
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    auto aivNum = ascendcPlatform.GetCoreNumAiv();

    uint32_t totalDataNum = context->GetInputTensor(1)->GetShapeSize();

    const gert::StorageShape *x0Shape = context->GetInputShape(0);
    const gert::StorageShape *x1Shape = context->GetInputShape(1);
    uint32_t b = x0Shape->GetStorageShape().GetDim(0);
    uint32_t n = x0Shape->GetStorageShape().GetDim(1);
    uint32_t m = x1Shape->GetStorageShape().GetDim(1);
    
    auto dt = context->GetInputDesc(0)->GetDataType();
    uint32_t sizeofdatatype;
    if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16) 
    {
        sizeofdatatype = 2;
    }
    else 
    {
        sizeofdatatype = 4;
    }

    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
    int32_t NUM = 6; //申请的内存块的个数（包括buffer和queue）
    uint32_t ub_block_num = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    ub_block_num = ub_block_num - (ub_block_num % 2 ? 1 : 0);
    uint32_t ub_data_num = ub_block_num * ALIGN_NUM;

    // 需要让ub_data_num跟ALIGN_NUM和3对齐
    ub_data_num = (ub_data_num / (ALIGN_NUM * 3)) * (ALIGN_NUM * 3);
    // ub_data_num = 24;


    uint32_t batchDataNum = totalDataNum / b;
    uint32_t batchDataNumAligned = (batchDataNum / 3 + ALIGN_NUM - 1) / ALIGN_NUM * ALIGN_NUM * 3;
    uint32_t blockDim = (aivNum < batchDataNumAligned / ub_data_num) ? aivNum : (batchDataNumAligned / ub_data_num);
    blockDim = blockDim >= 1 ? blockDim : 1;
    context->SetBlockDim(blockDim);

    uint32_t core_size = (batchDataNumAligned / blockDim) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);
    uint32_t core_remain = batchDataNumAligned - blockDim * core_size;

    printf("ALIGN_NUM:%d\n", ALIGN_NUM);
    printf("ub_data_num:%d\n", ub_data_num);
    printf("b:%d\n", b);
    printf("m:%d\n", m);
    printf("n:%d\n", n);
    printf("batchDataNum:%d\n", batchDataNum);
    printf("batchDataNumAligned:%d\n", batchDataNumAligned);
    printf("core_size:%d\n", core_size);
    printf("core_remain:%d\n", core_remain);



    tiling.set_ALIGN_NUM(ALIGN_NUM);
    tiling.set_ub_data_num(ub_data_num);
    tiling.set_b(b);
    tiling.set_m(m);
    tiling.set_n(n);
    tiling.set_batchDataNum(batchDataNum);
    tiling.set_batchDataNumAligned(batchDataNumAligned);
    tiling.set_core_size(core_size);
    tiling.set_core_remain(core_remain);

    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class ThreeNN : public OpDef {
public:
    explicit ThreeNN(const char* name) : OpDef(name)
    {
        this->Input("xyz1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Input("xyz2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Output("dist")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});
        this->Output("indices")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32})
            .Format({ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(ThreeNN);
}
